package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user/")
//@CrossOrigin(origins = "http://localhost:9090")//允许跨域 单独配置跨域问题
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public JsonResult login(@RequestBody @Validated UserLoginParam userLoginParam) {
        log.debug("Controller:用户登录:userLoginParam={}", userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "保存新员工信息或修改员工信息")
    @PostMapping("save")
    public JsonResult saveUser( @Validated UserSaveParam userSaveParam) {
        log.debug("Controller:保存新员工信息:userSaveParam={}", userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "显示用户列表")
    @GetMapping("select")
    public JsonResult select(UserQuery userQuery) {
        log.debug("Controller:userQuery={}", userQuery);
        List<UserVO> UserVOlist = userService.selectAll(userQuery);
        return JsonResult.ok(UserVOlist);
    }

    @ApiOperation(value = "重置密码")//
    @PostMapping("update/password/{userId}")
    public JsonResult update(@PathVariable Long userId) {
        log.debug("Controller:重置密码:userId={}",userId);
        userService.update(userId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改用户状态")
    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status) {
        log.debug("Controller:修改状态:userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("deleteUser/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("Controller:删除用户:userId={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId) {
        log.debug("Controller:查询审批人:parentId={}",parentId);
        List<UserVO> list = userService.selectAudit(parentId);
        return JsonResult.ok(list);
    }

}
